package com.miguo.component.search;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.widget.TextView;

import com.miguo.bean.CommentBean;
import com.miguo.bean.SearchKeyBean;
import com.miguo.framework.request.JsonData;
import com.miguo.framework.request.RequestJsonHandler;
import com.miguo.http.HttpClient;
import com.miguo.http.Url;

import java.util.ArrayList;
import java.util.List;

/**
 * File Names: SearchTxtCycleShowView
 * Describe:
 * User: LinXiaoZhi(87100854@qq.com)
 * Date: 2015-07-25
 * Time: 19:49
 */
public class SearchTxtCycleShowView extends TextView {

    private Context mContext;
    private int position = 0;
    private String Keyword = null;
    private Handler handler;
    private long interval = 5000;
    private static final int SCROLL_WHAT = 0;
    private List<SearchKeyBean> mKeywordsList;

    public SearchTxtCycleShowView(Context context) {
        this(context, null);
    }

    public SearchTxtCycleShowView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        this.mContext = context;
        handler = new ScrollHandler();
        mKeywordsList = new ArrayList<SearchKeyBean>();
        initKeyword();
    }

    public String getKeyword() {
        return this.Keyword;
    }

    // 获取搜索关键字
    private void initKeyword() {

        RequestJsonHandler handler = new RequestJsonHandler() {
            @Override
            public void onRequestFinish(JsonData json) {
                if (json.optInt("status") == 200) {
                    JsonData data = json.optJson("data");
                    mKeywordsList = CommentBean.SearchKey(data);
                    if (mKeywordsList != null && mKeywordsList.size() > 0) {
                        sendScrollMessage();
                    }
                }
            }
        };

        HttpClient.post(Url.toRoute(Url.Search.Keywords, 1), handler);
    }


    private void sendScrollMessage() {
        if (mKeywordsList.size() > 0) {
            position++;
            if (position >= mKeywordsList.size()) {
                position = 0;
            }
            Keyword = mKeywordsList.get(position).getName();
            setText("搜索“" + Keyword + "”");
            handler.removeMessages(SCROLL_WHAT);
            handler.sendEmptyMessageDelayed(SCROLL_WHAT, interval);
        }
    }

    private class ScrollHandler extends Handler {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case SCROLL_WHAT:
                    sendScrollMessage();
                    break;
                default:
                    break;
            }
        }
    }

    public void startAutoScroll() {
        sendScrollMessage();
    }

    public void stopAutoScroll() {
        handler.removeMessages(SCROLL_WHAT);
    }

    @Override
    public void onWindowFocusChanged(boolean hasWindowFocus) {
        super.onWindowFocusChanged(hasWindowFocus);
        if (hasWindowFocus) {
            startAutoScroll();
            return;
        }
        stopAutoScroll();
    }
}
